package com.amoyt.project.service.admin.impl;

import com.amoyt.project.config.redis.RedisIdWork;
import com.amoyt.project.constants.IdConstants;
import com.amoyt.project.constants.ResponseConstants;
import com.amoyt.project.domain.bo.LoginUser;
import com.amoyt.project.domain.dto.insert.AssistanceAddDto;
import com.amoyt.project.domain.dto.query.AssistancePageQuery;
import com.amoyt.project.domain.dto.update.AssistanceUpdateDto;
import com.amoyt.project.domain.pojo.AssistanceMeasure;
import com.amoyt.project.domain.res.PageResult;
import com.amoyt.project.domain.res.Result;
import com.amoyt.project.mapper.AssistanceMapper;
import com.amoyt.project.service.admin.AssistanceService;
import com.amoyt.project.utils.ConvertBeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 帮扶措施表 服务实现类
 * </p>
 *
 * @author AmoyT
 * @since 2025-08-21
 */
@Service
public class AssistanceServiceImpl implements AssistanceService {

    @Autowired
    private AssistanceMapper assistanceMapper;
    @Autowired
    private RedisIdWork redisIdWork;

    @Override
    public Result list(AssistancePageQuery query) {
        IPage<AssistanceMeasure> page = new Page<>(query.getPage(), query.getPageSize());
        QueryWrapper<AssistanceMeasure> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("household_id",query.getHouseholdId());
        queryWrapper.eq("plan_id",query.getPlanId());
        queryWrapper.eq("is_verified",query.getIsVerified());
        queryWrapper.like(query.getExecutor() != null, "executor", query.getExecutor());
        queryWrapper.like(query.getExecuteUnit() != null, "execute_unit", query.getExecuteUnit());
        queryWrapper.between(query.getStartTime() != null && query.getEndTime() != null, "execute_date", query.getStartTime(), query.getEndTime());
        queryWrapper.orderByDesc("execute_date");

        IPage<AssistanceMeasure> queryPage = assistanceMapper.selectPage(page, queryWrapper);

        PageResult pageResult = PageResult.builder()
                .total(queryPage.getTotal())
                .records(queryPage.getRecords())
                .build();

        return Result.success(pageResult);
    }

    @Override
    public Result update(AssistanceUpdateDto assistanceUpdateDto) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        AssistanceMeasure measure = ConvertBeanUtils.convert(assistanceUpdateDto, AssistanceMeasure.class);

        measure.setUpdateTime(LocalDateTime.now());
        measure.setUpdateBy(loginUser.getUser().getUserId());
        assistanceMapper.updateById(measure);
        return Result.success(ResponseConstants.UPDATE_SUCCESS);
    }

    @Override
    public Result add(AssistanceAddDto assistanceAddDto) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        AssistanceMeasure measure = ConvertBeanUtils.convert(assistanceAddDto, AssistanceMeasure.class);
        String measureId = IdConstants.ASSISTANCE_ID_PREFIX + redisIdWork.nextId(IdConstants.ASSISTANCE_ID_PREFIX);
        measure.setMeasureId(measureId);
        measure.setCreateTime(LocalDateTime.now());
        measure.setCreateBy(loginUser.getUser().getUserId());
        assistanceMapper.insert(measure);
        return Result.success(ResponseConstants.ADD_SUCCESS);
    }

    @Override
    public Result delete(List<String> ids) {
        //TODO 可能要做是否能删除判断
        assistanceMapper.deleteBatchIds(ids);
        return Result.success(ResponseConstants.DELETE_SUCCESS);
    }
}
